package thread;

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @since Oct 21, 2017
 */
public class Atomic implements Runnable {
    private int i = 0;

    //public synchronized int getValue() {
    public int getValue() {
        return i;
    }

    private synchronized void evenIncrement() {
        i++;
        i++;
    }

    public void run() {
        while (true) {
            evenIncrement();
        }
    }

    public static void main(String[] args) {
        ExecutorService exec = Executors.newCachedThreadPool();
        Atomic at = new Atomic();
        exec.execute(at);
        while (true) {
            int val = at.getValue();
            if (val % 2 != 0) {
                //[output]: (one of the output) 175
                //[reason]: getValue() is not synchronized, which made the value to be read when instability.
                System.out.println(val);
                System.exit(0);
            }
        }
    }
}
